package com.spdg.ring.util;

import java.io.File;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.view.View;
import cn.wolf.http.ProgressListener;
import cn.wolf.http.entity.BitmapProgress;
import cn.wolf.tools.AppHelper;
import cn.wolf.tools.CommonUtils;
import cn.wolf.tools.ImageFactory;
import cn.wolf.tools.SharedPreferencesUtil;

import com.spdg.ring.R;
import com.spdg.ring.common.Config;
import com.spdg.ring.common.Key;
import com.spdg.ring.datamgr.BitmapCacheMgr;

public class ImageLoaderGrid extends ImageFactory {

	private static ImageLoaderGrid mInstance = new ImageLoaderGrid();

	public static ImageLoaderGrid getInstance() {
		if (mInstance == null)
			mInstance = new ImageLoaderGrid();
		return mInstance;
	}

	@Override
	public String getOperateDir() {
		return Config.SDCARD_ARTICLE_PIC;
	}

	@Override
	public void cacheBitmap(Bitmap cache, String cacheKey) {
		BitmapCacheMgr.getInstance().saveBitmap(cacheKey, cache);
	}

	@Override
	public Bitmap getCache(String cacheKey) {
		return BitmapCacheMgr.getInstance().getBitmap(cacheKey);
	}

	@Override
	public int bitmapResourceIdDef() {
		return R.drawable.default_image;
	}

	// 无压缩图片
	public Bitmap loadBitmapOvri(Context context, final View view, final String url,
			final String cacheKey, final int resId) {
		mContext = context;
		return super.loadBitmap(view, url, cacheKey, resId);
	}

	@Override
	protected Bitmap loadBitmap(final View view, final String url, final String cacheKey,
			final ProgressListener<BitmapProgress> listener, boolean isCompress, int junst,
			int resId) {

		String dir = getOperateDir();
		String path = dir + "/" + cacheKey;

		if (CommonUtils.is3g2g(mContext)) {
			boolean imageSetting = SharedPreferencesUtil.getBoolean(mContext,
					Key.K_2G3G_IMAGE_SETTING);
			if (imageSetting) {
				File file = new File(path);
				if (file.exists()) {
					file.delete();
				}
			}
		}

		return super.loadBitmap(view, url, cacheKey, listener, isCompress, junst, resId);
	}

	@Override
	protected void setImageView(String cacheKey, View view, Bitmap bitmap, int resId) {
		if (bitmap == null)
			return;
		Bitmap bitmap1 = bitmap;
		int screenWidth = AppHelper.getScreenWidth(mContext);
		int bitmapWidth = bitmap.getWidth();
		if (screenWidth != bitmapWidth) {
			bitmap1 = zoomIn(bitmap, screenWidth);
			bitmapWidth = bitmap1.getWidth();
			int height = bitmap1.getHeight();
			System.out.println("height" + height);
			System.out.println("bitmapWidth:" + bitmapWidth);
		}
		super.setImageView(cacheKey, view, bitmap1, resId);
	}

	private Bitmap zoomIn(Bitmap bmp, int screenWidth) {
		int bmpWidth = bmp.getWidth();
		int bmpHeight = bmp.getHeight();
		if (screenWidth > bmpWidth) {
			float scale = screenWidth / (float) bmpWidth;
			Matrix matrix = new Matrix();
			matrix.postScale(scale, scale);
			bmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
		} else {
			float scale = screenWidth / (float) bmpWidth;
			Matrix matrix = new Matrix();
			matrix.postScale(scale, scale);
			bmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
		}

		return bmp;
	}
}
